/**
 * Copyright © 2024, ZhiYun Network Co.,Ltd. All rights reserved.
 * ZhiYun PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.itcoon.cloud.framework.transform.converter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;

/**
 * ClassName:com.itcoon.converter.transform.framework.ConverterFactory
 * Description:
 *
 * Date: 10/21/24 8:40 PM
 *
 * @author: miaoxia.yu
 *
 * @version 1.0.0
 */
public class ConverterFactory {

    public static final Logger log = LoggerFactory.getLogger(ConverterFactory.class);

    public final List<Converter> converters;

    public ConverterFactory(List<Converter> converters) {
        this.converters = converters;
    }

    public <T, R> Converter<T, R> getConverter(Object source, Class<?> targetClass){
        Optional<Converter> converterOp = converters.stream().filter(converter -> converter.maches(source, targetClass)).findAny();
        if(converterOp.isPresent()){
           return converterOp.get();
        }
        log.error("Not found a converter to process class#{}", targetClass);
        throw new UnsupportedOperationException();
    }
}
